﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GameNodeWithHandler
{
    class CreateNode
    {
        List<ConsoleNode> listOfGameNodes = new List<ConsoleNode>();
        Random rnd = new Random();
        int charNumber = 65;
        byte consoleColorCounter = 2;

        public void FillListOfNodes (GameField field, int amountOfNodes)
        {
            listOfGameNodes.Add(new ConsoleNode(rnd.Next(field.StartX, field.StartX + field.Width - 1), rnd.Next(field.StartY, field.StartY + field.Height - 1),
                (char) charNumber, (ConsoleColor)consoleColorCounter));
            for (int i = 1; i < amountOfNodes; i++)
            {
                listOfGameNodes.Add(GenerateUniqueNode(field));
            }
        }

        public List<ConsoleNode> GetListOfNodes ()
        {
            return listOfGameNodes;
        }

        public ConsoleNode GenerateUniqueNode(GameField field)
        {
            bool done = false;
            int newNodeX;
            int newNodeY;
            do
            {
                newNodeX = rnd.Next(field.StartX, field.StartX + field.Width - 1);
                newNodeY = rnd.Next(field.StartY, field.StartY + field.Height - 1);
                if (isUniqueCoordinates(newNodeX, newNodeY))
                {
                    if(consoleColorCounter==15)
                    { consoleColorCounter = 1; }
                    charNumber++;
                    consoleColorCounter++;
                    done = true;
                }
            }
            while (done == false);
            return new ConsoleNode(newNodeX, newNodeY, (char) charNumber, (ConsoleColor) consoleColorCounter);
        }

        private bool isUniqueCoordinates (int coordX, int coordY)
        {
            foreach (var node in listOfGameNodes)
            {
                if (coordY != node.Y && coordX != node.X)
                    return true;
            }
                return false;
        }

        public ConsoleNode CreateTargetNode(GameField field)
        {
            return GenerateUniqueNode(field);
        }

        public bool IsOnField(ConsoleNode node, GameField field, int methodChoise)
        {
            bool matchOnField = false;
            switch (methodChoise)
            {
                //1 - движение вверх
                case 1:
                    {
                        if (node.Y > field.StartY)
                        {
                            matchOnField = true;
                        }
                        break;
                    }
                //2 - движение вниз
                case 2:
                    {
                        if (node.Y < field.StartY + field.Height - 1)
                        {
                            matchOnField = true;
                        }
                        break;
                    }
                //3 - движение влево
                case 3:
                    {
                        if (node.X > field.StartX)
                        {
                            matchOnField = true;
                        }
                        break;
                    }
                //4 - движение вправо
                case 4:
                    {
                        if (node.X < field.StartX + field.Width - 1)
                        {
                            matchOnField = true;
                        }
                        break;
                    }
            }
            return matchOnField;
        }

        public bool IsOnNode(List<ConsoleNode> listOfGameNodes, ConsoleNode node, int methodChoise)
        {
            bool matchOnNode = false;
            switch (methodChoise)
            {
                //1 - движение вверх
                case 1:
                    {
                        foreach (var everyGameNode in listOfGameNodes)
                        {
                            if (node.Y - 1 == everyGameNode.Y)
                            {
                                if (node.X == everyGameNode.X)
                                    matchOnNode = true;
                            }
                        }
                        break;
                    }
                //2 - движение вниз
                case 2:
                    {
                        foreach (var everyGameNode in listOfGameNodes)
                        {
                            if (node.Y + 1 == everyGameNode.Y)
                            {
                                if (node.X == everyGameNode.X)
                                    matchOnNode = true;
                            }
                        }
                        break;
                    }
                //3 - движение влево
                case 3:
                    {
                        foreach (var everyGameNode in listOfGameNodes)
                        {
                            if (node.X - 1 == everyGameNode.X)
                            {
                                if (node.Y == everyGameNode.Y)
                                    matchOnNode = true;
                            }
                        }
                        break;
                    }
                //4 - движение вправо
                case 4:
                    {
                        foreach (var everyGameNode in listOfGameNodes)
                        {
                            if (node.X + 1 == everyGameNode.X)
                            {
                                if (node.Y == everyGameNode.Y)
                                    matchOnNode = true;
                            }
                        }
                        break;
                    }
            }
            return matchOnNode;
        }
        
    }
}
